﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.Master
{
    public partial class TemplateEdit : PageBase
    {
        public string FJID { set; get; }
        AttachmentTemplateBO bo = new AttachmentTemplateBO();
        LogBO lbo = new LogBO();
        T_FileBO manager = new T_FileBO();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["DID"] != null)
                {
                    LoadDep(Request.QueryString["DID"].ToString());
                }
                //LoadData();
            }
            //if (Request.QueryString["deid"] != null)
            //{
            //    dele(Request.QueryString["deid"].ToString());
            //}
            if (Request.QueryString["deid"] != null)
            {
                DeleteFile(Request.QueryString["deid"].ToString());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileid"></param>
        private void DeleteFile(string fileid)
        {
            try
            {
                DataSet ds = manager.Getpath(int.Parse(fileid));
                string path = ds.Tables[0].Rows[0]["F_Detail"].ToString();

                if (manager.DeleteFile(fileid))
                {
                    // File.Delete(path);
                    
                    AlertReLoad("Deleted successful");
                    //AlertRedirect("Deleted successful", "Template.aspx");
                    //AlertBack("Deleted successful");
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        //private void dele(string id)
        //{
        //    int num = bo.Delete(int.Parse(id));
        //    lbo.WriteLog(CurrentUser.Name, "Delete Template");
        //    if (num > 0)
        //        AlertMessage("Deleted successful");
        //    LoadData();
        //}

        //private void LoadData()
        //{
        //    DataSet ds = bo.GetList();
        //    GridView1.DataSource = ds;
        //    GridView1.DataBind();
        //}

        private void LoadDep(string id)
        {
            Model.AttachmentTemplate d = bo.GetModel(int.Parse(id));
            txtAttachment.Text = d.Attachmenttype;
            txtRemark.Text = d.REMARK;
            ddlTemplateAccepted.SelectedValue = d.SUPPLIERSTEMPLATEACCEPTED.ToString();
            ddlTemplateAvailable.SelectedValue = d.BDNTTEMPLATEAVAILABLE.ToString();
            cbLevel1.Checked = d.ISLEVEL1 == 1 ? true : false;
            cbLevel2.Checked = d.ISLEVEL2 == 1 ? true : false;
            cbLevel3.Checked = d.ISLEVEL3 == 1 ? true : false;
            Button1.Visible = true;
            FJID = d.UPLOADEDFILE;
            //btnUp.Visible = true;
            lblUp.Text = "<input type='button'   class='button'  onclick='javascript:PopUpWindow(\"UploadFile.aspx?FJID=" + d.UPLOADEDFILE + "\", 230, 160, 730, 350);'   value='Upload'>";
            DataSet ds = manager.GetFileList(d.UPLOADEDFILE);
            if (ds.Tables[0].Rows.Count == 0)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr["F_Filename"] = "<font color='red'>Not upload files</font>";
                ds.Tables[0].Rows.Add(dr);
            }
            GridView2.DataSource = ds;
            GridView2.DataBind();
            Button1.Text = "Save";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Model.AttachmentTemplate d = new Model.AttachmentTemplate();
            if (Request.QueryString["DID"] != null)
                d = bo.GetModel(int.Parse(Request.QueryString["DID"].ToString()));
            int num = 0;
            d.REMARK = txtRemark.Text;
            d.SUPPLIERSTEMPLATEACCEPTED = int.Parse(ddlTemplateAccepted.SelectedValue);
            d.BDNTTEMPLATEAVAILABLE = int.Parse(ddlTemplateAvailable.SelectedValue);
            d.ISLEVEL1 = cbLevel1.Checked ? 1 : 0;
            d.ISLEVEL2 = cbLevel2.Checked ? 1 : 0;
            d.ISLEVEL3 = cbLevel3.Checked ? 1 : 0;
            d.LASTMODIFIEDDATE = DateTime.Now;
            if (Button1.Text == "Add")
            {
                num = bo.Add(d);
                lbo.WriteLog(CurrentUser.Id, "Add Template:" + d.Attachmenttype);
            }
            else
            {
                d.ID = int.Parse(Request.QueryString["DID"].ToString());
                num = bo.Update(d);
                lbo.WriteLog(CurrentUser.Id, "Edit Template:" + d.Attachmenttype);
            }
            if (num > 0)
            {
                txtAttachment.Text = "";
                txtRemark.Text = "";
                Button1.Text = "Add";
                Button1.Visible = false;
                //AlertMessage("Save success");
            }
            AlertReflashOpener("Save success");
            //LoadData();
        }

    }
}